/*
 * get_local_internet_addr uses the gethostname function and 
 * gethostbyname function to retrieve the internet address of 
 * the local host (the first one encountered is used).  The 
 * host address is copied into the user-provided storage 
 * pointed to by the calling parameter.
 *
 *  usage:
 *  get_local_internet_addr(&place_to_stick_addr);
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <string.h>
#include "tp_debug.h"

extern int gethostname (char *name, size_t namelen);
uint32_t inet_addr (const char *cp);


#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 80
#endif /* MAXHOSTNAMELEN */

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif /* INADDR_NONE */

struct hostent *hp;
static char hostname[MAXHOSTNAMELEN + 1];

int get_local_internet_addr (char *storage)
{
  if (gethostname (hostname, sizeof (hostname)) == -1) {
    LOG ("get_local_internet_addr:  could not find local host name\n");
    return (-1);
  }
  hostname[MAXHOSTNAMELEN] = '\0';  /* null terminate */

  if ((hp = gethostbyname (hostname)) == NULL) {
    LOG ("gethostbyname error for %s\n", hostname);
    return (-1);
  }
  memcpy (storage, hp->h_addr, hp->h_length);
  return (0);
}

int32_t get_remote_internet_addr (char *hostname)
{
  uint32_t temp_inaddr;

  if ((temp_inaddr = inet_addr (hostname)) != INADDR_NONE)
    return (temp_inaddr);
  else {
    if ((hp = gethostbyname (hostname)) == NULL) {
      LOG ("Could not get remote internet address for %s\n", hostname);
      exit (-1);
    }
    memcpy ((char *) &temp_inaddr, hp->h_addr, hp->h_length);
    return (temp_inaddr);
  }
}
